package pri.damai.xiaowu.anydoor.netty;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import pri.damai.xiaowu.anydoor.constant.ChatConst;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 保存链接映射关系
 *
 * @description
 * @author YangXinFu
 * @date 11:49 2022/7/25
 **/
public interface ChannelStore {

    /** key:userId,value:channel */
    Map<Integer, Channel> USER_CHANNEL_MAP = new ConcurrentHashMap<Integer, Channel>();
    Map<Channel, Integer> CHANNEL_USER_MAP = new ConcurrentHashMap<Channel, Integer>();

    /** key:groupId,value:userIds */
    Map<String, List<Integer>> GROUP_USER_MAP = new ConcurrentHashMap<>();

    /** key:groupId,value:groupName */
    Map<String, String> GROUP_NAME_MAP = new ConcurrentHashMap<>();

    static Channel getUserChannel(Integer userId) {
        return USER_CHANNEL_MAP.get(userId);
    }

    static void setUserChannel(Integer userId,Channel channel) {
        USER_CHANNEL_MAP.put(userId,channel);
        channel.attr(AttributeKey.valueOf(ChatConst.USER_ID_KEY)).set(userId);
        CHANNEL_USER_MAP.put(channel,userId);
    }


}
